package bootstrap.liftweb

import _root_.net.liftweb.util._
import _root_.net.liftweb.http._
import _root_.net.liftweb.sitemap._
import _root_.net.liftweb.sitemap.Loc._
import Helpers._
import _root_.net.liftweb.mapper._
import _root_.java.sql.{Connection, DriverManager}
import _root_.swta.hellolift.model._

/**
  * A class that's instantiated early and run.  It allows the application
  * to modify lift's environment
  */
class Boot {
  def boot {
    // add the connection manager if there's not already a JNDI connection defined
    if (!DB.jndiJdbcConnAvailable_?) DB.defineConnectionManager(DefaultConnectionIdentifier, DBVendor)

    // add the com.hellolift package to the list packages
    // searched for Snippets, CometWidgets, etc.
    LiftRules.addToPackages("swta.hellolift")

    // Update the database schema to be in sync
    Schemifier.schemify(true, Log.infoF _, /* User, */ Entry)

    // The locale is calculated based on the http request
    LiftRules.localeCalculator = r => LiftRules.defaultLocaleCalculator(r)

    // Build SiteMap
    val entries = Menu(Loc("Home", List("index"), "Home")) ::
        Entry.sitemap

    LiftRules.setSiteMap(SiteMap(entries:_*))
  }
}

object DBVendor extends ConnectionManager {
  def newConnection(name: ConnectionIdentifier): Can[Connection] = {
    try {
      Class.forName("org.h2.Driver")
      val dm =  DriverManager.getConnection("jdbc:h2:./supersimple")
      Full(dm)
    } catch {
      case e : Exception => e.printStackTrace; Empty
    }
  }
  def releaseConnection(conn: Connection) {conn.close}
}